package com.hsjk.szwj.module.train.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.hsjk.szwj.module.train.controller.app.vo.question.AppQuestionSetDetailRespVO;
import com.hsjk.szwj.module.train.convert.QuestionSetConvert;
import com.hsjk.szwj.module.train.dal.dataobject.question.QuestionDO;
import com.hsjk.szwj.module.train.dal.dataobject.question.QuestionSetDO;
import com.hsjk.szwj.module.train.dal.dataobject.question.QuestionSetMappingDO;
import com.hsjk.szwj.module.train.dal.mysql.question.IQuestionMapper;
import com.hsjk.szwj.module.train.dal.mysql.question.IQuestionSetMapper;
import com.hsjk.szwj.module.train.dal.mysql.question.IQuestionSetMappingMapper;
import com.hsjk.szwj.module.train.service.IQuestionSetService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * @author chenjiawei
 * @desc
 * @Date 2023/11/04 21:55
 **/
@Service
public class QuestionSetServiceImpl implements IQuestionSetService {
    @Resource
    private IQuestionSetMapper questionSetMapper;
    @Resource
    private IQuestionMapper questionMapper;

    @Resource
    private IQuestionSetMappingMapper questionSetMappingMapper;

    @Override
    public AppQuestionSetDetailRespVO getQuestionSetDetailById(Long id) {
        QuestionSetDO questionSetDO = questionSetMapper.selectById(id);
        return QuestionSetConvert.INSTANCE.convertAppQuestionSetDetailRespVO(questionSetDO);
    }

    @Override
    public void transformSpaceQuestionSet() {
        List<Long> regionIds = new ArrayList<>();
        regionIds.add(1716791333995286533L);
        regionIds.add(1716791333995286566L);
        regionIds.add(1716791333995286577L);
        List<QuestionSetDO> questionSetDOS = questionSetMapper.selectBatchIds(regionIds);
        if (CollUtil.isNotEmpty(questionSetDOS)) {
            List<QuestionDO> questionDOList = new ArrayList<>();
            for (QuestionSetDO setDO : questionSetDOS) {
                List<QuestionSetMappingDO> questionSetMappingDOS = questionSetMappingMapper.selectList(QuestionSetMappingDO::getQuestionSetId, setDO.getId());
                for (QuestionSetMappingDO mappingDO : questionSetMappingDOS) {
                    QuestionDO questionDO = new QuestionDO();
                    questionDO.setId(mappingDO.getQuestionId());
                    questionDO.setRegionType(2);
                    questionDOList.add(questionDO);
                }
            }
            if (CollUtil.isNotEmpty(questionDOList)) {
                questionMapper.updateBatch(questionDOList, questionDOList.size());
            }
        }
    }
}
